<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="上报时间">
        <el-date-picker
          v-model="daterangeReportingDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="设备ID" prop="machineryId">
        <el-input
          v-model="queryParams.machineryId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="机台号" prop="machineryCode">
        <el-input
          v-model="queryParams.machineryCode"
          placeholder="请输入机台号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="设备名称" prop="machineryName">
        <el-input
          v-model="queryParams.machineryName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="原纸名称" prop="baselPaperName">
        <el-input
          v-model="queryParams.baselPaperName"
          placeholder="请输入原纸名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="克重" prop="gramWeight">
        <el-input
          v-model="queryParams.gramWeight"
          placeholder="请输入克重"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="切前宽" prop="widthBeforeCutting">
        <el-input
          v-model="queryParams.widthBeforeCutting"
          placeholder="请输入切前宽"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重量" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="件数" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入件数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材料用途" prop="materialUsage">
        <el-input
          v-model="queryParams.materialUsage"
          placeholder="请输入材料用途"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="切后宽" prop="widthAfterCutting">
        <el-input
          v-model="queryParams.widthAfterCutting"
          placeholder="请输入切后宽"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合格卷数" prop="numberQualifiedRolls">
        <el-input
          v-model="queryParams.numberQualifiedRolls"
          placeholder="请输入合格卷数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="理论米数" prop="theoreticalMeters">
        <el-input
          v-model="queryParams.theoreticalMeters"
          placeholder="请输入理论米数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重量(KG)" prop="weightKg">
        <el-input
          v-model="queryParams.weightKg"
          placeholder="请输入重量(KG)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报表长度" prop="reportLength">
        <el-input
          v-model="queryParams.reportLength"
          placeholder="请输入报表长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="短溢数" prop="shortOverflowNumber">
        <el-input
          v-model="queryParams.shortOverflowNumber"
          placeholder="请输入短溢数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算米数" prop="settlementMeters">
        <el-input
          v-model="queryParams.settlementMeters"
          placeholder="请输入结算米数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="作业员" prop="operator">
        <el-input
          v-model="queryParams.operator"
          placeholder="请输入作业员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="计件工资" prop="pieceRateWage">
        <el-input
          v-model="queryParams.pieceRateWage"
          placeholder="请输入计件工资"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="异常数" prop="heteroconstant">
        <el-input
          v-model="queryParams.heteroconstant"
          placeholder="请输入异常数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产情况" prop="productionSituation">
        <el-input
          v-model="queryParams.productionSituation"
          placeholder="请输入生产情况"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注1" prop="note1">
        <el-input
          v-model="queryParams.note1"
          placeholder="请输入备注1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注2" prop="note2">
        <el-input
          v-model="queryParams.note2"
          placeholder="请输入备注2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fb:slitting:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fb:slitting:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fb:slitting:export']"
        >导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
                   v-hasPermi="['mes:fb:slitting:import']">导入</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                 :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                 :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-table v-loading="loading" :data="slittingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="记录ID" align="center" fixed  prop="recordId" /> -->
      <el-table-column label="年" align="center" prop="year"  fixed="left"/>
      <el-table-column label="月" align="center" prop="month"  fixed="left"/>
      <el-table-column label="日" align="center" prop="day"  fixed="left"/>
      <!-- <el-table-column label="上报时间" align="center" prop="reportingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="设备ID" align="center" prop="machineryId" /> -->
      <el-table-column label="机台号" align="center" prop="machineryCode" />
      <!-- <el-table-column label="设备名称" align="center" prop="machineryName" /> -->
      <el-table-column label="原纸名称" align="center" prop="baselPaperName" />
      <el-table-column label="克重" align="center" prop="gramWeight" />
      <el-table-column label="切前宽" align="center" prop="widthBeforeCutting" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="件数" align="center" prop="number" />
      <el-table-column label="材料用途" align="center" prop="materialUsage" />
      <el-table-column label="切后宽" align="center" prop="widthAfterCutting" />
      <el-table-column label="合格卷数" align="center" prop="numberQualifiedRolls" />
      <el-table-column label="理论米数" align="center" prop="theoreticalMeters" />
      <el-table-column label="重量(KG)" align="center" prop="weightKg" />
      <el-table-column label="报表长度" align="center" prop="reportLength" />
      <el-table-column label="短溢数" align="center" prop="shortOverflowNumber" />
      <el-table-column label="结算米数" align="center" prop="settlementMeters" />
      <el-table-column label="作业员" align="center" prop="operator" />
      <el-table-column label="计件工资" align="center" prop="pieceRateWage" />
      <el-table-column label="异常数" align="center" prop="heteroconstant" />
      <el-table-column label="生产情况" align="center" prop="productionSituation" />
      <el-table-column label="备注1" align="center" prop="note1" />
      <el-table-column label="备注2" align="center" prop="note2" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fb:slitting:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fb:slitting:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改大分切对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" inline :model="form" :rules="rules" label-width="120px">
        <el-form-item label="年" prop="year">
          <el-input v-model="form.year" type="number" placeholder="请输入年" />
        </el-form-item>
        <el-form-item label="月" prop="month">
          <el-input v-model="form.month" type="number" placeholder="请输入月" />
        </el-form-item>
        <el-form-item label="日" prop="day">
          <el-input v-model="form.day" type="number" placeholder="请输入日" />
        </el-form-item>
        <!-- <el-form-item label="上报时间" prop="reportingDate">
          <el-date-picker clearable
            v-model="form.reportingDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上报时间">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="设备ID" prop="machineryId">
          <el-input v-model="form.machineryId" placeholder="请输入设备ID" />
        </el-form-item> -->
        <el-form-item label="机台号" prop="machineryCode">
          <el-input v-model="form.machineryCode" type="number" placeholder="请输入机台号" />
        </el-form-item>
        <!-- <el-form-item label="设备名称" prop="machineryName">
          <el-input v-model="form.machineryName" placeholder="请输入设备名称" />
        </el-form-item> -->
        <el-form-item label="原纸名称" prop="baselPaperName">
          <el-input v-model="form.baselPaperName" placeholder="请输入原纸名称" />
        </el-form-item>
        <el-form-item label="克重" prop="gramWeight">
          <el-input v-model="form.gramWeight" type="number" placeholder="请输入克重" />
        </el-form-item>
        <el-form-item label="切前宽" prop="widthBeforeCutting">
          <el-input v-model="form.widthBeforeCutting" type="number" placeholder="请输入切前宽" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" type="number" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="件数" prop="number">
          <el-input v-model="form.number" type="number" placeholder="请输入件数" />
        </el-form-item>
        <el-form-item label="材料用途" prop="materialUsage">
          <el-input v-model="form.materialUsage" placeholder="请输入材料用途" />
        </el-form-item>
        <el-form-item label="切后宽" prop="widthAfterCutting">
          <el-input v-model="form.widthAfterCutting" type="number" placeholder="请输入切后宽" />
        </el-form-item>
        <el-form-item label="合格卷数" prop="numberQualifiedRolls">
          <el-input v-model="form.numberQualifiedRolls" type="number" placeholder="请输入合格卷数" />
        </el-form-item>
        <el-form-item label="理论米数" prop="theoreticalMeters">
          <el-input v-model="form.theoreticalMeters" type="number" placeholder="请输入理论米数" />
        </el-form-item>
        <el-form-item label="重量(KG)" prop="weightKg">
          <el-input v-model="form.weightKg" type="number" placeholder="请输入重量(KG)" />
        </el-form-item>
        <el-form-item label="报表长度" prop="reportLength">
          <el-input v-model="form.reportLength" type="number" placeholder="请输入报表长度" />
        </el-form-item>
        <el-form-item label="短溢数" prop="shortOverflowNumber">
          <el-input v-model="form.shortOverflowNumber" type="number" placeholder="请输入短溢数" />
        </el-form-item>
        <el-form-item label="结算米数" prop="settlementMeters">
          <el-input v-model="form.settlementMeters" type="number" placeholder="请输入结算米数" />
        </el-form-item>
        <el-form-item label="作业员" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入作业员" />
        </el-form-item>
        <el-form-item label="计件工资" prop="pieceRateWage">
          <el-input v-model="form.pieceRateWage" type="number" placeholder="请输入计件工资" />
        </el-form-item>
        <el-form-item label="异常数" prop="heteroconstant">
          <el-input v-model="form.heteroconstant" type="number" placeholder="请输入异常数" />
        </el-form-item>
        <el-form-item label="生产情况" prop="productionSituation">
          <el-input v-model="form.productionSituation" placeholder="请输入生产情况" />
        </el-form-item>
        <el-form-item label="备注1" prop="note1">
          <el-input v-model="form.note1" placeholder="请输入备注1" />
        </el-form-item>
        <el-form-item label="备注2" prop="note2">
          <el-input v-model="form.note2" placeholder="请输入备注2" />
        </el-form-item>
        <!-- <el-form-item label="数据行状态(0:正常/1:删除)" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入数据行状态(0:正常/1:删除)" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSlitting, getSlitting, delSlitting, addSlitting, updateSlitting } from "@/api/mes/fb/slitting";
import {getToken} from "@/utils/auth";
import {parseTime} from "@/utils/ruoyi";

export default {
  name: "Slitting",
  data() {
    return {
      // 遮罩层
      loading: true,
      daterangeReportingDate:[],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 大分切表格数据
      slittingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportingDate: null,        machineryId: null,        machineryCode: null,        machineryName: null,        baselPaperName: null,        gramWeight: null,        widthBeforeCutting: null,        weight: null,        number: null,        materialUsage: null,        widthAfterCutting: null,        numberQualifiedRolls: null,        theoreticalMeters: null,        weightKg: null,        reportLength: null,        shortOverflowNumber: null,        settlementMeters: null,        operator: null,        pieceRateWage: null,        heteroconstant: null,        productionSituation: null,        note1: null,        note2: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
            { required: true, message: "年不能为空", trigger: "blur" }
          ],     
          month: [
            { required: true, message: "月不能为空", trigger: "blur" }
          ],     
          day: [
            { required: true, message: "日不能为空", trigger: "blur" }
          ], 
      },
      // 导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {
          Authorization: "Bearer " + getToken()
        },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/mes/fb/slitting/importData"
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询大分切列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
        if (null != this.daterangeReportingDate && '' != this.daterangeReportingDate) {
            this.queryParams.params["beginReportingDate"] = this.daterangeReportingDate[0];
            this.queryParams.params["endReportingDate"] = this.daterangeReportingDate[1];
        }
      listSlitting(this.queryParams).then(response => {
        this.slittingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "报表导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      // this.download('/mes/fb/flow/importTemplate', {}, `fbReel_template_${new Date().getTime()}.xlsx`)
      this.download('/mes/fb/slitting/importTemplate', {}, `大分切报表模板.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        year: null, 
        month: null,
        day: null,
        recordId: null,        reportingDate: null,        machineryId: null,        machineryCode: null,        machineryName: null,        baselPaperName: null,        gramWeight: null,        widthBeforeCutting: null,        weight: null,        number: null,        materialUsage: null,        widthAfterCutting: null,        numberQualifiedRolls: null,        theoreticalMeters: null,        weightKg: null,        reportLength: null,        shortOverflowNumber: null,        settlementMeters: null,        operator: null,        pieceRateWage: null,        heteroconstant: null,        productionSituation: null,        note1: null,        note2: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null,        delFlag: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeReportingDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加大分切";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getSlitting(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改大分切";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateSlitting(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSlitting(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm('是否确认删除大分切编号为"' + recordIds + '"的数据项？').then(function() {
        return delSlitting(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.beforeExportFn();
      this.download('mes/fb/slitting/export', {
        ...this.queryParams
      }, `大分切报表_${parseTime(new Date().getTime(), '{y}-{m}-{d}-{h}')}.xlsx`)
    }
  }
};
</script>
